package com.manga.util;

import org.springframework.stereotype.Service;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class FileService {

    private Map<Path, List<String>> cache = new HashMap<>();

    public List<String> files(Path path) {
        if (cache.containsKey(path)) {
            return cache.get(path);
        }

        try {
            List<String> list = Files.list(path).map(Path::getFileName).map(Path::toString).collect(Collectors.toList());
            cache.put(path, list);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
